// createRouter创建路由实例
// createWebHistory创建history模式路由
import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/login/index.vue'
import Layout from '@/views/layout/index.vue'
import Home from '@/views/Home/index.vue'
import Category from '@/views/Category/index.vue'
import SubCategotry from '@/views/SubCategory/index.vue'

const router = createRouter({
  // 创建路由模式
  history: createWebHistory(import.meta.env.BASE_URL),
  // path和component对应关系的位置
  routes: [
    {
      // 进来会直接进入 layout 组件
      path: '/',
      component: Layout,
      children: [
        {
          // 默认的二级页面是Home组件
          path: '',
          component: Home,
        },
        {
          // 导航栏中的各项，在指定路径上加一个占位 id 参数
          path: 'category/:id',
          component: Category,
        },
        {
          // 这里面跳转的是二级分类页面里面的业务模块,这里路径前半部分一样是为了做激活样式的判断
          path: 'category/:id/sub/:subId',
          component: SubCategotry,
        },
        {
          path: 'cartlist',
          component: () => import('@/views/CartList/index.vue'),
        },
        {
          path: 'checkout',
          component: () => import('@/views/Checkout/index.vue'),
        },
        {
          path: 'pay',
          component: () => import('@/views/Pay/index.vue'),
        },
        {
          path: 'paycallback',
          component: () => import('@/views/Pay/PayBack.vue'),
        },
        {
          path: 'detail/:id',
          component: () => import('@/views/Detail/index.vue'),
        },
        {
          path: 'member',
          component: () => import('@/views/Member/index.vue'),
          // redirect: '/member/user',
          children: [
            {
              path: '',
              component: () => import('@/views/Member/components/UserInfo.vue'),
            },
            {
              path: 'order',
              component: () => import('@/views/Member/components/UserOrder.vue'),
            },
          ],
        },
      ],
    },
    {
      path: '/login',
      component: Login,
    },
  ],
  // 路由滚动行为定制,这里控制的是跳转后的页面位置
  scrollBehavior() {
    // 返回值是页面滚动的位置
    return {
      top: 0,
    }
  },
})

export default router
